package com.ansion.baseui.base.toolbar;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import com.ansion.utils.ValueSafeTransfer;

import java.util.List;


/**
 * created by zhaoyuntao
 * on 05/01/2023
 * description:
 */
public class TMenuItem {
    private String text;

    @StringRes
    private Integer textRes;
    @ColorInt
    private Integer textColor;
    @ColorRes
    private Integer textColorRes;

    @DrawableRes
    private Integer drawableRes;
    @DrawableRes
    private Integer drawableBackgroundRes;
    private Integer drawablePadding;
    private String drawableURL;
    @ColorInt
    private Integer drawableColor;
    private LinearLayout.LayoutParams drawableLayoutParams;
    @ColorRes
    private Integer drawableColorRes;
    private Drawable drawable;
    private int orderId;
    private View.OnClickListener listener;

    private List<TMenuItem> popMenus;

    private UICallback uiCallback;

    private String menuItemTag;//用于标记菜单项的唯一标识

    public TMenuItem() {
    }

    public TMenuItem setListener(View.OnClickListener listener) {
        this.listener = listener;
        return this;
    }

    public View.OnClickListener getListener() {
        return listener;
    }


    public TMenuItem setTextRes(@StringRes int textRes) {
        this.textRes = textRes;
        return this;
    }

    public TMenuItem setText(@NonNull String text) {
        this.text = text;
        return this;
    }

    public TMenuItem setDrawableRes(@DrawableRes int drawableRes) {
        this.drawableRes = drawableRes;
        return this;
    }

    public TMenuItem setDrawable(Drawable drawable) {
        this.drawable = drawable;
        return this;
    }

    public TMenuItem setOrderId(int orderId) {
        this.orderId = orderId;
        return this;
    }

    public TMenuItem setTextColorRes(@ColorRes int textColorRes) {
        this.textColorRes = textColorRes;
        return this;
    }

    public TMenuItem setTextColor(@ColorInt int textColor) {
        this.textColor = textColor;
        return this;
    }

    public TMenuItem setDrawableColorRes(@ColorRes int drawableColorRes) {
        this.drawableColorRes = drawableColorRes;
        return this;
    }

    public TMenuItem setDrawableColor(@ColorInt int drawableColor) {
        this.drawableColor = drawableColor;
        return this;
    }

    public TMenuItem setDrawableBackground(@DrawableRes int drawableBackgroundRes) {
        this.drawableBackgroundRes = drawableBackgroundRes;
        return this;
    }
    public TMenuItem setDrawablePadding(int drawablePadding) {
        this.drawablePadding = drawablePadding;
        return this;
    }

    public TMenuItem setDrawableLayoutParams(LinearLayout.LayoutParams layoutParams) {
        this.drawableLayoutParams = layoutParams;
        return this;
    }

    public TMenuItem setDrawableLayoutParams(int width, int height, int marginStartEnd) {
        setDrawableLayoutParams(width, height, marginStartEnd, marginStartEnd);
        return this;
    }

    public TMenuItem setDrawableLayoutParams(int width, int height, int marginStartStart, int marginStartEnd) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
        lp.gravity = Gravity.CENTER_VERTICAL;
        lp.setMarginStart(marginStartStart);
        lp.setMarginEnd(marginStartEnd);
        this.drawableLayoutParams = lp;
        return this;
    }

    public LinearLayout.LayoutParams getDrawableLayoutParams() {
        return drawableLayoutParams;
    }

    @ColorInt
    public Integer getTextColor() {
        return textColor;
    }

    @ColorInt
    public Integer getDrawableColor() {
        return drawableColor;
    }

    @ColorRes
    public Integer getDrawableColorRes() {
        return drawableColorRes;
    }

    @ColorRes
    public Integer getTextColorRes() {
        return textColorRes;
    }

    public boolean hasTextColor() {
        return textColor != null;
    }

    public boolean hasTextColorRes() {
        return textColorRes != null;
    }

    public boolean isEnable() {
        return true;
    }

    public Integer getTextRes() {
        return textRes;
    }

    public Integer getDrawableRes() {
        return drawableRes;
    }
    public Integer getDrawableBackgroundRes() {
        return drawableBackgroundRes;
    }
    public Integer getDrawablePadding() {
        return drawablePadding;
    }

    public Drawable getDrawable() {
        return drawable;
    }

    public int getOrderId() {
        return orderId;
    }

    public String getText() {
        return text;
    }

    public List<TMenuItem> getPopMenus() {
        return ValueSafeTransfer.transformList(popMenus, (position, t) -> t.isVisible() ? t : null);
    }

    public TMenuItem setPopMenus(List<TMenuItem> popMenus) {
        this.popMenus = popMenus;
        return this;
    }

    public TMenuItem setMenuItemTag(String menuItemTag) {
        this.menuItemTag = menuItemTag;
        return this;
    }
    public String getMenuItemTag() {
        return menuItemTag;
    }

    public boolean hasPopMenu() {
        return this.popMenus != null && !this.popMenus.isEmpty();
    }

    public boolean hasText() {
        return !TextUtils.isEmpty(getText());
    }

    public boolean hasTextRes() {
        return getTextRes() != null;
    }

    public boolean hasDrawableRes() {
        return getDrawableRes() != null;
    }
    public boolean hasDrawableBackgroundRes() {
        return getDrawableBackgroundRes() != null;
    }
    public boolean hasDrawablePadding() {
        return getDrawablePadding() != null;
    }
    public boolean hasDrawable() {
        return getDrawable() != null;
    }

    public boolean hasDrawableColor() {
        return getDrawableColor() != null;
    }

    public boolean hasDrawableColorRes() {
        return getDrawableColorRes() != null;
    }

    public String getDrawableURL() {
        return drawableURL;
    }

    public void setDrawableURL(String drawableURL) {
        this.drawableURL = drawableURL;
    }

    public boolean hasDrawableLayoutParams() {
        return getDrawableLayoutParams() != null;
    }

    public boolean hasDrawableURL() {
        return !TextUtils.isEmpty(getDrawableURL());
    }

    public boolean isVisible() {
        return true;
    }

    public void notifyUIChanged() {
        if (uiCallback != null) {
            uiCallback.onNotifyChanged(TMenuItem.this);
        }
    }

    public void setUiCallback(UICallback uiCallback) {
        this.uiCallback = uiCallback;
    }

    public interface UICallback {
        void onNotifyChanged(@NonNull TMenuItem menuItem);
    }

    @Override
    public String toString() {
        return "TTMenuItem{" +
                ", text='" + text + '\'' +
                ", drawableRes=" + drawableRes +
                ", orderId=" + orderId +
                '}';
    }
}
